/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package doan.quanlycanbo.portlet.service.impl;

import java.util.List;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.portlet.PortletClassLoaderUtil;
import com.liferay.portal.model.Organization;
import com.liferay.portal.model.ResourceConstants;

import com.liferay.portal.service.OrganizationLocalServiceUtil;
import com.liferay.portal.service.persistence.OrganizationPersistence;

import doan.quanlycanbo.portlet.NoSuchdonviException;
import doan.quanlycanbo.portlet.model.donvi;
import doan.quanlycanbo.portlet.model.hoso;
import doan.quanlycanbo.portlet.model.hosoWrapper;
import doan.quanlycanbo.portlet.service.donviLocalServiceUtil;
import doan.quanlycanbo.portlet.service.hosoLocalServiceUtil;
import doan.quanlycanbo.portlet.service.base.donviLocalServiceBaseImpl;

/**
 * 
 * @author Nguyen Tuan Vinh
 */
public class donviLocalServiceImpl extends donviLocalServiceBaseImpl {
	
	public donvi geDepartmentByD_G(long departmentId, long groupId) throws NoSuchdonviException, SystemException{
		return donviPersistence.findByD_G(departmentId, groupId);
	}
	
	public List<donvi> getAll() throws SystemException{
		return donviPersistence.findAll();
	}
	
	public List<donvi> getByKhoiDV(long khoidv) throws SystemException{
		return donviPersistence.findByKhoiDV(khoidv);
	}
	
	public List<donvi> getByGroup(long groupId) throws SystemException{
		return donviPersistence.findByGroup(groupId);
	}
	
	public List<donvi> getByParent(long parentId) throws SystemException{
		return donviPersistence.findByParent(parentId);
	}
	
	/**
	 * Danh sách viện trưởng hoặc viện phó của đơn vị
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<hoso> getDsLanhDao(long depId, long funcId) throws SystemException{
		DynamicQuery query = DynamicQueryFactoryUtil.forClass(hoso.class, PortletClassLoaderUtil.getClassLoader());
		query.add(PropertyFactoryUtil.forName("madonvi").eq(new Long(depId)));
		query.add(PropertyFactoryUtil.forName("machucvu").eq(new Long(funcId)));
		List<hoso> hoso = hosoLocalServiceUtil.dynamicQuery(query);
		return hoso;
	}
	
	public int countAllEmployee(long depId) throws SystemException{
		int count = 0;
		List<hoso> hoso = hosoLocalServiceUtil.getByDonVi(depId);
		count = hoso.size();
		return count;
	}
	
	/*
	 * Danh sach can bo theo don vi va hoc ham
	 */
	@SuppressWarnings("unchecked")
	public List<hoso> getDsHocham(long depId, long mahocham) throws SystemException{
		DynamicQuery query = DynamicQueryFactoryUtil.forClass(hoso.class, PortletClassLoaderUtil.getClassLoader());
		query.add(PropertyFactoryUtil.forName("madonvi").eq(new Long(depId)));
		query.add(PropertyFactoryUtil.forName("mahocham").eq(new Long(mahocham)));
		List<hoso> hoso = hosoLocalServiceUtil.dynamicQuery(query);
		return hoso;
	}
	
	public int countDsHocham(long depId, long mahocham) throws SystemException{
		int count = getDsHocham(depId, mahocham).size();
		return count;
	}
	
	/*
	 * Danh sach can bo theo don vi va hoc vi
	 */
	@SuppressWarnings("unchecked")
	public List<hoso> getDsHocvi(long depId, long mahocvi) throws SystemException{
		DynamicQuery query = DynamicQueryFactoryUtil.forClass(hoso.class, PortletClassLoaderUtil.getClassLoader());
		query.add(PropertyFactoryUtil.forName("madonvi").eq(new Long(depId)));
		query.add(PropertyFactoryUtil.forName("mahocvi").eq(new Long(mahocvi)));
		List<hoso> hoso = hosoLocalServiceUtil.dynamicQuery(query);
		return hoso;
	}
	
	public int countDsHocvi(long depId, long mahocvi) throws SystemException{
		int count = getDsHocvi(depId, mahocvi).size();
		return count;
	}
	
	public donvi addDonVi(donvi dv1, long userId) throws SystemException{
		long madonvi = CounterLocalServiceUtil.increment(donvi.class.getName());
		donvi dv2 = donviPersistence.create(madonvi);
		
		dv2.setGroupId(dv1.getGroupId());
		dv2.setCompanyId(dv1.getCompanyId());
		dv2.setLanguageId(dv1.getLanguageId());
		dv2.setMakhoi(dv1.getMakhoi());
		dv2.setDonvicha(dv1.getDonvicha());
		dv2.setTendonvi(dv1.getTendonvi());
		dv2.setDiachi(dv1.getDiachi());
		dv2.setDienthoai(dv1.getDienthoai());
		dv2.setFax(dv1.getFax());
		dv2.setEmail(dv1.getEmail());
		dv2.setWebsite(dv1.getWebsite());
		dv2.setHinhanh(dv1.getHinhanh());
		dv2.setGioithieuchung(dv1.getGioithieuchung());
		
		try {
			resourceLocalService.addResources(dv1.getCompanyId(), dv1.getGroupId(), userId, 
					donvi.class.getName(), dv2.getPrimaryKey(), false, true, true);
		} catch (PortalException e) {
			e.printStackTrace();
		}
		
		long orgId = CounterLocalServiceUtil.increment(Organization.class.getName());
		Organization org = OrganizationLocalServiceUtil.createOrganization(orgId);
		org.setName(dv2.getTendonvi());
		org.setType("regular-organization");
		org.setCompanyId(dv2.getCompanyId());
		org.setRecursable(true);
		org.setStatusId(12017);
		dv2.setOrgId(orgId);
		donviPersistence.update(dv2, false);
		
		
		if(dv2.getDonvicha() == 0){
			org.setParentOrganizationId(0);
		}else{
			try {
				donvi dvParent = donviLocalServiceUtil.getdonvi(dv2.getDonvicha());
				org.setParentOrganizationId(dvParent.getOrgId());
			} catch (PortalException e) {
				e.printStackTrace();
			}
		}
		
		
		OrganizationLocalServiceUtil.addOrganization(org);
		
		return dv2;
	}
	
	public donvi updateDonVi(long madonvi, long makhoi, long donvicha, String tendonvi,
			String diachi, String dienthoai, String fax, String hinhanh, String gioithieuchung) throws NoSuchdonviException, SystemException{
		
		donvi dv = donviPersistence.findByPrimaryKey(madonvi);
		dv.setMakhoi(makhoi);
		dv.setDonvicha(donvicha);
		dv.setTendonvi(tendonvi);
		dv.setDiachi(diachi);
		dv.setDienthoai(dienthoai);
		dv.setFax(fax);
		dv.setHinhanh(hinhanh);
		dv.setGioithieuchung(gioithieuchung);
		return dv;
	}
	
	public void deleteDonVi(long madonvi) throws NoSuchdonviException, SystemException,PortalException{
		donvi dv = donviPersistence.findByPrimaryKey(madonvi);
		deleteDonVi(dv);
	}
	
	public void deleteDonVi(donvi dv) throws SystemException, PortalException{
		resourceLocalService.deleteResource(dv.getCompanyId(), donvi.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, dv.getPrimaryKey());
		donviPersistence.remove(dv);
	}
}


















